/**
 * @author yxw
 * date : 2013年10月19日 下午4:56:25 
 */
package com.tzinfo.tzt.util;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.util.EncodingUtils;

public class CodingTools {
	// 编码转换工具
	public static String getStr_UTF_8(String input) {
		String result = null;
		try {
			if (input != null) {
				result = new String(input.getBytes("iso-8859-1"), "UTF-8");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public static String getStr_GBK(String input) {
		String result = null;
		try {
			if (input != null) {
				result = new String(input.getBytes("iso-8859-1"), "GBK");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public static String getStr_GB2312(String input) {
		String result = null;
		try {
			if (input != null) {
				result = new String(input.getBytes("iso-8859-1"), "GB2312");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public static String changeCNCode(String str, String code) {
		String res = str;
		
		Pattern p = Pattern.compile("([\u4e00-\u9fa5]+)");
		Matcher m = p.matcher(str);
		String mv = null;
		String old = null;
		while (m.find()) {
			mv = m.group(0);
			old=mv;
			try {
				
				mv=URLEncoder.encode(mv, code);
				res.replaceAll(old, mv);
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return null;
			}
		}
		return res;
	}

	public static List<String> findCNworld(String str) {
		List<String> list = new ArrayList<String>();
		Pattern p = Pattern.compile("([\u4e00-\u9fa5]+)");
		Matcher m = p.matcher(str);
		String mv = null;

		while (m.find()) {

			mv = m.group(0);
			list.add(mv);
		}
		return list.size() > 0 ? list : null;
	}
}
